/****************************************************************************
 * Copyright (C) 2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * DeviceWindow.cpp
 *
 * for SaveGame Manager GX 2013
 ***************************************************************************/
#include "DeviceWindow.hpp"
#include "../../Themes/CTheme.h"
#include "../../Controls/Application.h"

DeviceWindow::DeviceWindow()
	: ManageWindow()
	, type(0)
{
	dlWindow = NULL;
	
	wifiBoxImg = new GuiImage(btnBoxData);
	wifiBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	wifiBoxImg->SetPosition(-60, 6);
	
	btnWifi = new PictureButton("button_wifi.png", "button_wifi_over.png", btnSoundClick, btnSoundOver);
	btnWifi->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnWifi->SetPosition(-63, 8);
	btnWifi->SetTrigger(&trigA);
	btnWifi->Clicked.connect(this, &DeviceWindow::OnWifiButtonClick);
	
	warningTxt = new GuiText(tr("WARNING: You need to have a save already installed in the Wii to install this one!!!"), 20, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	warningTxt->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	warningTxt->SetPosition(-6, -24);
	warningTxt->SetMaxWidth(450, SCROLL_WIDE);
	
	saveInfos = new SaveInfos();
	
	miiInfos = new MiiInfos();
	miiInfos->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	miiInfos->SetPosition(0, 65);
	
	saveButtons = new DevSaveButtons();
	saveButtons->BlockButtons.connect(this, &DeviceWindow::OnBlockButtons);
	saveButtons->EntryDeleted.connect(this, &DeviceWindow::OnEntryDeleted);
	
	miiButtons = new DevMiiButtons();
	miiButtons->BlockButtons.connect(this, &DeviceWindow::OnBlockButtons);
	miiButtons->EntryDeleted.connect(this, &DeviceWindow::OnEntryDeleted);
	
	databinButtons = new DevDatabinButtons();
	databinButtons->BlockButtons.connect(this, &DeviceWindow::OnBlockButtons);
	databinButtons->EntryDeleted.connect(this, &DeviceWindow::OnEntryDeleted);
	
	SetInfos();
	
	Refresh.connect(this, &DeviceWindow::OnRefresh);
	
	Application::Instance()->Append(this);
	Application::Instance()->SetUpdateOnly(this);
}

DeviceWindow::~DeviceWindow()
{
	RemoveAll();
	
	if(dlWindow)
		delete dlWindow;
	
	delete saveButtons;
	delete miiButtons;
	delete databinButtons;
	
	delete wifiBoxImg;
	
	delete btnWifi;
	
	delete warningTxt;
	
	delete saveInfos;
	delete miiInfos;
}

void DeviceWindow::SetInfos()
{
	Remove(wifiBoxImg);
	Remove(btnWifi);
	Remove(saveInfos);
	Remove(miiInfos);
	Remove(warningTxt);
	Remove(saveButtons);
	Remove(miiButtons);
	Remove(databinButtons);
	
	if(CurrentBrowser()->IsSave(selIndex))
		type = _SAVE_;
	else if(CurrentBrowser()->IsMii(selIndex))
		type = _MII_;
	else if(CurrentBrowser()->IsDataBin(selIndex))
		type = _DATABIN_;
	
	if(type == _SAVE_ || type == _DATABIN_)
	{
		saveInfos->SetInfos(selIndex);
		Append(saveInfos);
		
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_WIISAVE_DOWNLOAD))
		{
			Append(wifiBoxImg);
			Append(btnWifi);
		}
		
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
		{
			if(type == _SAVE_)
			{
				saveButtons->SetActionButtons();
				Append(saveButtons);
			}
			else
			{
				databinButtons->SetActionButtons();
				Append(databinButtons);
			}
			
			if(CurrentBrowser()->IsNotInstalled(selIndex))
				Append(warningTxt);
		}
	}
	else if(type == _MII_)
	{
		miiInfos->SetInfos(selIndex);
		Append(miiInfos);
		
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
			Append(miiButtons);
	}
}

void DeviceWindow::OnWifiButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	OnBlockButtons(true);
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
	{
		saveButtons->SetEffect(EFFECT_FADE, -20);
		while(saveButtons->IsAnimated())
			Application::Instance()->updateEvents();
		Remove(saveButtons);
	}
	
	dlWindow = new DownloadSaveWindow(saveInfos->GetTid(), saveInfos->GetID(), this);
	dlWindow->End.connect(this, &DeviceWindow::OnDownloadSaveEnd);
}

void DeviceWindow::OnDownloadSaveEnd()
{
	delete dlWindow;
	dlWindow = NULL;
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
	{
		saveButtons->SetEffect(EFFECT_FADE, 20);
		Append(saveButtons);
		while(saveButtons->IsAnimated())
			Application::Instance()->updateEvents();
	}
	
	OnBlockButtons(false);
}

void DeviceWindow::OnRefresh(bool inc)
{
	int entryCount = CurrentBrowser()->GetEntrieCount();
	int tries = 0;
	
	if(!entryCount)
	{
		closing = true;
		Close();
		return;
	}
	
	if(inc)
	{
		do
		{
			tries++;
			selIndex++;
			
			if(selIndex >= entryCount)
				selIndex = 0;
			if(tries > entryCount)
			{
				Close();
				return;
			}
			
		} while(!CurrentBrowser()->IsSave(selIndex) &&
				!CurrentBrowser()->IsMii(selIndex) &&
				!CurrentBrowser()->IsDataBin(selIndex));
	}
	else
	{
		do
		{
			tries++;
			selIndex--;
			
			if(selIndex < 0)
				selIndex = entryCount-1;
			if(tries > entryCount)
			{
				Close();
				return;
			}
			
		} while(!CurrentBrowser()->IsSave(selIndex) &&
				!CurrentBrowser()->IsMii(selIndex) &&
				!CurrentBrowser()->IsDataBin(selIndex));
	}
	
	CurrentBrowser()->SetSelectedIndex(selIndex);
	
	SetInfos();
}

void DeviceWindow::OnEntryDeleted()
{
	Explorer::Instance()->Reload();
	CurrentBrowser()->FilterList();
	
	selIndex--;
	POINT p = {0, 0};
	OnArrowButtonClick(btnRight, 0, p);
}

void DeviceWindow::OnBlockButtons(bool block)
{
	int State = STATE_DEFAULT;
	if(block)
		State = STATE_DISABLED;
	
	btnWifi->SetState(State);
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
	{
		if(CurrentBrowser()->IsSave(selIndex))
		{
			if(!block && CurrentBrowser()->IsNotInstalled(selIndex))
				Append(warningTxt);
			else
				Remove(warningTxt);
		}
	}
	
	ManageWindow::OnBlockButtons(block);
}
